<?php

class TopMenuWidget extends CWidget
{

    public $items;
    public $selectIds;

    private $_html = '';
	private $_aSexByCategory = array();

    public function init()
    {
        $oCategories = new Categories();
        $oPhotos = new Photos();
        $oCategoryProperty = new CategoryProperty();
	    $this->items = $items = $oCategories->getAll(null, true, true, 'goods');

	    $oPhotos->selectMainByRowset($this->items);

	    $ids = array_keys($this->items);
//	    print_r($ids); exit;
	    $aSex = $oCategoryProperty->getByType($ids, 2);
//	    print_r($aSex); exit;

	    $this->_aSexByCategory = array();
	    foreach($aSex as $i => $oRow)
	    {
		    $this->_aSexByCategory[$oRow['category_id']][$oRow['id']]['title'] = $oRow['value'];
		    $this->_aSexByCategory[$oRow['category_id']][$oRow['id']]['property_id'] = $oRow['property_id'];
	    }
//	    print_r($this->_aSexByCategory); exit;


//	    foreach($this->items as $i => $arr)
//		    $this->items[$i]['childNodes'] = $items;

//	    $oCategories = new Categories();

//	    $oCategoryProperty = new CategoryProperty();
//	    $this->items = $oCategoryProperty->getByType($this->selectIds, 2);

//	    print_r($this->items); exit;

//	    $ids = $oCategories->getAllChild($this->selectIds);
//print_r($this->selectIds); exit;
//	    $this->items = $oCategories->id($ids)->findAll();
//	    $this->items = $oCategories->getUsedCategories($ids);
//	    $this->items = $oCategories->toTree($this->items, 'id', 'relative_id');
	    
//        print_r($this->items); exit;
    }

    public function run()
    {
        $this->_tree($this->items);
    }

    protected function _tree($items, $level = 1, $oParentRow = null)
    {
	    $tag = $level == 1 ? 'menu' : 'ul';

	    if($oParentRow && !empty($this->_aSexByCategory[$oParentRow->id]))
	    {
		    $this->_html .= '<div class="sm" '. (!empty($oParentRow['photo']) ? 'style="background: #fff url('. $oParentRow['photo']['path'] .''. $oParentRow['photo']['file'] .') no-repeat right bottom;")' : '') .'>';
//		    $this->_html .= '<div class="sm">';
		    foreach($this->_aSexByCategory[$oParentRow->id] as $valueId => $aSex)
		    {
			    $request = '';
			    if($aSex['property_id'] > 0 && $valueId > 0)
				    $request = '?v='. $aSex['property_id'] .':'. $valueId;

		        $this->_html .= '<div class="sm-col"><div class="sm-title">'. $aSex['title'] .'</div>';

			    $this->_html .= '<'. $tag .'>';

		        foreach($items as $i => $oRow)
		        {
			        $this->_html .= '<li'. (isset($this->selectIds[$oRow->id]) ? ' class="selected"' : '') .'>';

		            $this->_html .= '<a href="'. Yii::app()->controller->createUrl('/'. ($oRow->type ? $oRow->type : 'category') .'/default/index', array('category_id' => $oRow->createUrlId())) . $request .'">'. $oRow->title .'</a>';

		            if(!empty($oRow->childNodes))
		            {
		                $this->_tree($oRow->childNodes, $level+1, $oRow);
		            }

		            $this->_html .= '</li>';
		        }

			    $this->_html .= '</'. $tag .'></div>';
		    }
		    $this->_html .= '</div>';
	    }
	    else
	    {
		    if($level > 1)
		    {
			    $this->_html .= '<div class="sm">';
			    $this->_html .= ' <div class="sm-col">';
		    }

		    $this->_html .= '<'. $tag .'>';

		    foreach($items as $i => $oRow)
		    {
			    $this->_html .= '<li'. (isset($this->selectIds[$oRow->id]) ? ' class="selected"' : '') .'>';

			    $this->_html .= '<a href="'. Yii::app()->controller->createUrl('/'. ($oRow->type ? $oRow->type : 'category') .'/default/index', array('category_id' => $oRow->createUrlId())) .'">'. $oRow->title .'</a>';

			    if(!empty($oRow->childNodes))
			    {
				    $this->_tree($oRow->childNodes, $level+1, $oRow);
			    }

			    $this->_html .= '</li>';
		    }

		    $this->_html .= '<li class="right page"><a href="/">Скидки</a></li>';
		    $this->_html .= '<li class="right page"><a href="/usefully_politika-vozvrata-tovara">Возврат 365 дней</a></li>';
		    $this->_html .= '<li class="right page"><a href="/usefully_dostavka-i-oplata-tovarov">Бесплатная доставка</a></li>';

		    $this->_html .= '</'. $tag .'>';

		    if($level > 1)
		    {
		        $this->_html .= '</div></div>';
		    }
	    }


        return $this->_html;
    }

    public function __toString()
    {
        return $this->_html;
    }


}